﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HomePage.Controllers;
using PortalNews.BUS;
using TGVR_Project.Models;
using System.IO;
using PortalNews.BlogspotNews;
using PortalNews.WordpressNews;
using MDF.Utilites.Helper;
using DataHelper;

namespace TGVR_Project.Controllers
{
    public class NewsController : BaseController
    {
        //
        // GET: /News/
        private int m_iTypeNews = 1;
        private string m_strController = "bigsale";
        public ActionResult Index(int _iPage = 1)
        {
            int iNumPage = 20;
            NewsBUS nBUS = new NewsBUS();

            ViewBag.BigSaleNews = nBUS.GetBigSaleNews(m_lang.iLang, 20);

            New[] lstData = nBUS.GetAll(m_iTypeNews, m_lang.iLang);

            if (Request.QueryString["keyword"] != null)
            {
                string strKeyword = Request.QueryString["keyword"].Trim().ToLower();
                strKeyword = StringHelper.ToNonUnicodeAndLower(strKeyword);
                lstData = lstData.Where(m => StringHelper.ToNonUnicodeAndLower(m.Title).IndexOf(strKeyword) > -1).ToArray();
            }

            ViewBag.LatestNews = lstData.Skip((_iPage - 1) * iNumPage).Take(iNumPage).ToArray();
            m_Pagging.IsUssing = true;
            if (Request.QueryString["keyword"] != null)
            {
                string strKeyword = Request.QueryString["keyword"].Trim().ToLower();
                m_Pagging.Link = m_seo.Domain + "/news/" + m_strController + "?keyword=" + strKeyword + "&page=";
            }
            else
            {
                m_Pagging.Link = m_seo.Domain + "/news/" + m_strController + "/";
            }
            m_Pagging.TotalPage = lstData.Length / iNumPage + 1;
            m_Pagging.CurrentPage = _iPage;

            ViewBag.m_iTypeNews = m_iTypeNews;

            return View("Index");
        }
        public ActionResult BigSale(int id = 1)
        {
            m_strController = "BigSale";
            m_iTypeNews = 2;
            ViewBag.NewsTitle = m_lang.BIGSALE;
            if (Request.QueryString["page"] != null)
            {
                string strPage = Request.QueryString["page"].Trim();
                return Index(Int32.Parse(strPage));
            }
            ViewBag.Title = "Tin ve khuyen mai " + m_seo.Title;
            return Index(id);
        }

        public ActionResult Travel(int id = 1)
        {
            m_strController = "Travel";
            ViewBag.NewsTitle = m_lang.TRAVEL;
            m_iTypeNews = 12;
            if (Request.QueryString["page"] != null)
            {
                string strPage = Request.QueryString["page"].Trim();
                return Index(Int32.Parse(strPage));
            }
            ViewBag.Title = "Tin ve du lich khuyen mai " + m_seo.Title;
            return Index(id);
        }

        public ActionResult Ourwork(int id = 1)
        {
            m_strController = "Ourwork";
            m_iTypeNews = 10;
            ViewBag.NewsTitle = m_lang.OURWORK;
            if (Request.QueryString["page"] != null)
            {
                string strPage = Request.QueryString["page"].Trim();
                return Index(Int32.Parse(strPage));
            }
            ViewBag.Title = "Nghiep vu ve may bay " + m_seo.Title;
            return Index(id);
        }

        public ActionResult ReSale(int id = 1)
        {
            m_strController = "ReSale";
            ViewBag.NewsTitle = m_lang.RESALE;
            m_iTypeNews = 11;
            if (Request.QueryString["page"] != null)
            {
                string strPage = Request.QueryString["page"].Trim();
                return Index(Int32.Parse(strPage));
            }
            ViewBag.Title = "Nhuong ve may bay " + m_seo.Title;
            return Index(id);
        }



        public ActionResult Detail(string id, Comment comment = null)
        {
            CommentBUS cBus = new CommentBUS();
            string[] arrTemp = id.Split('-');
            int newsID = Int32.Parse(arrTemp[arrTemp.Length - 1]);

            if (comment == null || comment.NewsID == 0)
            {
                comment = new Comment();
                comment.NewsID = newsID;
            }
            else
            {
                //lưu comment vào database
                comment.LangID = m_lang.iLang;

                if (comment.Phone.IndexOf("http") > -1)
                    return null;
                if (comment.Email.IndexOf("http") > -1)
                    return null;
                comment.IP = Request.UserHostAddress;
                if (cBus.Insert(comment))
                {
                    comment = new Comment();
                    comment.NewsID = newsID;
                }
            }

            if (Request.QueryString["read"] != null)
            {
                cBus = new CommentBUS();
                int iCommentID = Int32.Parse(Request.QueryString["read"]);
                cBus.SetRead(iCommentID);
            }
            NewsBUS nBUS = new NewsBUS();
            New news = nBUS.GetByID(newsID, m_lang.iLang);
            m_seo.Keywords = news.SEO + "," + m_seo.Keywords;
            ViewBag.OrderNews = nBUS.GetOlderNews(m_lang.iLang, news, 10);
            ViewBag.Comment = comment;
            ViewBag.ListComment = cBus.Select(newsID, m_lang.iLang);
            ViewBag.Title = "Tin ve khuyen mai ," + news.Title + news.SEO + " ," + m_seo.Title;
            return View(news);
        }
    }
}
